import {ComponentsInfoTypes, ComponentsStateType} from "./index";

export function getNextSelected(fe_id: string, componentsList: ComponentsInfoTypes[]) {
  const index = componentsList.filter(c => !c.isHidden).findIndex(c => c.fe_id === fe_id)
  // 没有选中要删除的id
  if (index < 0) return ''

  //重新计算selectedId
  let selectedId = ''
  const length = componentsList.length
  if (length <= 1) {
    // 组件只有1个
    selectedId = ''
  } else {
    if (index + 1 === length) {
      selectedId = componentsList[index - 1].fe_id
    } else {
      selectedId = componentsList[index + 1].fe_id
    }
  }
  return selectedId
}

export function insertNewComponent(drart: ComponentsStateType, newComponent: ComponentsInfoTypes) {
  const {selectedId} = drart
  const index = drart.componentList.findIndex(c => c.fe_id === selectedId)
  if (index < 0) {
    drart.componentList.push(newComponent)
  } else {
    drart.componentList.splice(index + 1, 0, newComponent)
  }
  drart.selectedId = newComponent.fe_id
}
